'''
Created on 01/04/2010

@author: miguel
'''
import pygame
from Instruction import Instruction
from InstructionPanelRow import InstructionPanelRow

class InstructionsPanel(object):
    (EMPTY, CREATING) = range(2)
    UP_ARROW = './data/up_arrow.png'
    DOWN_ARROW = './data/down_arrow.png'
    LEFT_ARROW = './data/left_arrow.png'
    RIGHT_ARROW = './data/right_arrow.png'
    STOP_SIGN = './data/stop_sign.png'
    EXPLOSION_SIGN = './data/explosion_sign.png'
    PLUS_SIGN = './data/plus_sign.png'
    MINUS_SIGN = './data/minus_sign.png'
    CREATE_BUTTON = './data/create_button.png'
    LAUNCH_BUTTON = './data/launch_button.png'
    REMOVE_BUTTON = './data/remove_button.png'
    ADD_BUTTON = './data/add_button.png'
    FONT_FILE_NAME = './data/Always_Joking.ttf'

    def __init__(self, screen, game):
        self.screen = screen
        self.game = game
        self.status = self.EMPTY
        self.font = pygame.font.Font(self.FONT_FILE_NAME, 24)
        self.font.set_bold(True)

        self.image_up_arrow = pygame.image.load(self.UP_ARROW)
        self.image_down_arrow = pygame.image.load(self.DOWN_ARROW)
        self.image_left_arrow = pygame.image.load(self.LEFT_ARROW)
        self.image_right_arrow = pygame.image.load(self.RIGHT_ARROW)
        self.image_stop_sign = pygame.image.load(self.STOP_SIGN)
        self.image_explosion_sign = pygame.image.load(self.EXPLOSION_SIGN)
        self.image_action_button = self.image_up_arrow
        self.rect_action_button = pygame.Rect(570, 456, 30, 24)

        self.image_plus_sign = pygame.image.load(self.PLUS_SIGN)
        self.rect_plus_sign = pygame.Rect(690, 456, 30, 24)
        self.image_minus_sign = pygame.image.load(self.MINUS_SIGN)
        self.rect_minus_sign = pygame.Rect(720, 456, 30, 24)
        self.image_add_button = pygame.image.load(self.ADD_BUTTON)
        self.rect_add_button = pygame.Rect(750, 456, 50, 24)

        self.image_create = pygame.image.load(self.CREATE_BUTTON)
        self.image_launch = pygame.image.load(self.LAUNCH_BUTTON)
        self.image_create_launch = self.image_create
        self.rect_create_launch = pygame.Rect(700, 120, 100, 24)

        self.image_remove_button = pygame.image.load(self.REMOVE_BUTTON)

        self.instruction_panel_row_list = []
        self.number_of_instructions = 0
        self.instruction = Instruction.MOVE_UP
        self.instruction_time = 1
        self.instruction_time_text = '  %d secs.'%self.instruction_time
        self.time = 0
        self.time_text = 'Time: %d'%self.time
        self.time_counter = 0

    def update(self):
        if self.time_counter > 30:
            self.time_counter = 0
            self.time = self.time + 1
            self.time_text = 'Time: %d'%self.time
        self.time_counter = self.time_counter + 1

    def __click_over_action(self):
        if self.instruction == Instruction.MOVE_UP:
            self.instruction = Instruction.MOVE_RIGHT
            self.image_action_button = self.image_right_arrow
        elif self.instruction == Instruction.MOVE_RIGHT:
            self.instruction = Instruction.MOVE_DOWN
            self.image_action_button = self.image_down_arrow
        elif self.instruction == Instruction.MOVE_DOWN:
            self.instruction = Instruction.MOVE_LEFT
            self.image_action_button = self.image_left_arrow
        elif self.instruction == Instruction.MOVE_LEFT:
            self.instruction = Instruction.STOP
            self.image_action_button = self.image_stop_sign
        elif self.instruction == Instruction.STOP:
            self.instruction = Instruction.MOVE_UP
            self.image_action_button = self.image_up_arrow

    def __click_over_plus_sign(self):
        if self.instruction_time <= 20 :
            self.instruction_time = self.instruction_time + 1
            self.instruction_time_text = '  %d secs.'%self.instruction_time

    def __click_over_minus_sign(self):
        if self.instruction_time > 1:
            self.instruction_time = self.instruction_time - 1
            self.instruction_time_text = '  %d secs.'%self.instruction_time

    def __click_over_add_button(self):
        if self.number_of_instructions <= 11:
            self.number_of_instructions = self.number_of_instructions + 1
            '''add a new instruction to list and draw it'''
            self.y = 456 - (self.number_of_instructions * 24)
            self.rect_action_image = pygame.Rect(570, self.y, 30, 24)
            self.rect_time_text = pygame.Rect(600, self.y, 170, 24)
            self.rect_remove_button = pygame.Rect(770, self.y, 30, 24)
            self.instruction_time_text = self.instruction_time_text
            self.instruction_panel_row = InstructionPanelRow(self.screen, self.number_of_instructions, self.image_action_button, self.rect_action_image, self.instruction_time_text, self.rect_time_text, self.image_remove_button, self.rect_remove_button, Instruction(self.instruction, self.instruction_time))
            self.instruction_panel_row_list.append(self.instruction_panel_row)

            '''initialize the generation of a new instruction'''
            self.instruction = Instruction.MOVE_UP
            self.image_action_button = self.image_up_arrow
            self.instruction_time = 1
            self.instruction_time_text = '  %d secs.'%self.instruction_time

    def __click_over_create_button(self):
        self.image_create_launch = self.image_launch
        self.status = self.CREATING

        self.instruction_list = []
        self.instruction_panel_row_list = []
        self.number_of_instructions = 0
        self.instruction = Instruction.MOVE_UP
        self.instruction_time = 1
        self.instruction_time_text = '  %d secs.'%self.instruction_time
        self.time_text = 'Time: %d'%self.time
        self.time_counter = 0

    def __click_over_launch_button(self):
        if self.game.player.bugs_number < 2:#if number of user bugs == 2,then you can launch the new bug until one of the bugs explode
            if len(self.instruction_panel_row_list) > 0:
                self.image_create_launch = self.image_create
                self.status = self.EMPTY
                instruction_list = []
                for instruction_panel_row in self.instruction_panel_row_list:
                    instruction_list.append(instruction_panel_row.instruction)
                '''create new bug'''
                instruction_list.append(Instruction(Instruction.EXPLODE, 1))
                    # instruction to create a new bug
                self.game.player.create_user_bug(instruction_list)
            # print list of instructions
            #for inst in self.instruction_list:
            #    inst.print_instruction()

    def __click_over_remove_button(self, id):
        '''adjust the rest of instructions'''
        for instruction_panel_row in self.instruction_panel_row_list[id:]:
            instruction_panel_row.id = instruction_panel_row.id - 1

            (x,y,width,height) = instruction_panel_row.rect_action_image
            y = y + 24
            instruction_panel_row.rect_action_image = pygame.Rect(570, y, 30, 24)
            instruction_panel_row.rect_text = pygame.Rect(600, y, 170, 24)
            instruction_panel_row.rect_remove_image = pygame.Rect(770, y, 30, 24)
        '''remove instruction'''
        self.instruction_panel_row_list.pop(id - 1)
        self.number_of_instructions = self.number_of_instructions - 1

    def click(self, click_position):
        if self.status == self.EMPTY:
            if self.rect_create_launch.collidepoint(click_position):
                self.__click_over_create_button()
        elif self.status == self.CREATING:
            if self.rect_create_launch.collidepoint(click_position):
                self.__click_over_launch_button()
            elif self.rect_action_button.collidepoint(click_position):
                self.__click_over_action()
            elif self.rect_plus_sign.collidepoint(click_position):
                self.__click_over_plus_sign()
            elif self.rect_minus_sign.collidepoint(click_position):
                self.__click_over_minus_sign()
            elif self.rect_add_button.collidepoint(click_position):
                self.__click_over_add_button()
            '''elif CHECKING OF BUTTON REMOVE'''
            for instruction_panel_row in self.instruction_panel_row_list:
                if instruction_panel_row.rect_remove_image.collidepoint(click_position):
                    self.__click_over_remove_button(instruction_panel_row.id)


    def draw(self):
        pygame.draw.rect(self.screen, (0, 0, 0), (570, 120, 230, 360))
        #pygame.draw.rect(self.screen, (200, 100, 255), (570, 120, 230, 360)) for tests
        self.screen.blit(self.image_create_launch, self.rect_create_launch)
        self.general_time = self.font.render(self.time_text, True, pygame.Color('blue'), (0,0,0))
        self.screen.blit(self.general_time, pygame.Rect(570,120,130,24))
        if self.status == self.CREATING:
            self.screen.blit(self.image_action_button, self.rect_action_button)
            self.screen.blit(self.image_add_button, self.rect_add_button)
            self.screen.blit(self.image_plus_sign, self.rect_plus_sign)
            self.screen.blit(self.image_minus_sign, self.rect_minus_sign)
            self.inst_time = self.font.render(self.instruction_time_text, True, pygame.Color('blue'), (0,0,0))
            self.screen.blit(self.inst_time, pygame.Rect(600,456,90,24))

            for instruction_panel_row in self.instruction_panel_row_list:
                instruction_panel_row.draw()
